package Niuke;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Random;

public class Q {



    public static boolean isPrime(int a){
        if (a==1)
            return true;
        for (int i=2;i<a;i++){
            if (a%i==0)
                return false;
        }
        return true;
    }

    public static void process(int[] arr){
        int sum=0;
        for (int num:arr)
            sum+=num;
        System.out.println(sum);
        Double average=sum*1.0/arr.length;
        System.out.println(average);
    }

    public void sort(int[] arr){
        int temp;
        for(int i=0;i<arr.length-1;i++){
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }


    //非递归
    public static int sum(int n){
        return (1+n)*n/2;
    }
    //递归
    public static int sum2(int n){
        if (n==1)
            return 1;
        return n+sum2(n-1);
    }

    public void showName(File f){
        if (f.isDirectory()){
            File[] files = f.listFiles();
            for (File file:files)
                System.out.println(file.getName());
        }
    }

    public void readFile(File file) throws Exception{
        FileInputStream is=new FileInputStream(file);
        InputStreamReader reader=new InputStreamReader(is,"GBK");
        BufferedReader br=new BufferedReader(reader);
        String line=null;
        while ((line=br.readLine())!=null) {
            System.out.println(line);
        }
        reader.close();
        br.close();
        is.close();
    }

    public void pickNames(String[] names){
        Random random=new Random();
        boolean[] selected=new boolean[names.length];
        int count=0;
        while (count<5){
            int idx = random.nextInt(10);
            if (idx< names.length&&selected[idx]==false){
                selected[idx]=true;
                System.out.println(names[idx]);
                count++;
            }
        }
    };


    static int curmoney=0;
    static final Object lock=new Object();
    public static  void put(int money){
        synchronized (lock){
            curmoney+=money;
        }
    }

    public static void get(int money){
        synchronized (lock){
            if (curmoney<money){
                System.out.println("余额不足");
            }else
                curmoney-=money;
        }
    }

    public int lengthOfLongestSubstring (String s) {
        // write code here
        char[] chars = s.toCharArray();
        int maxlen = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int index = 0;
        int curlen = 0;
        while (index < chars.length) {
            if (map.containsKey(chars[index])) {
                int lastidx = map.get(chars[index]);
                if (index - lastidx < curlen) {
                    curlen = index - lastidx;
                } else {
                    curlen++;
                }
                maxlen = Math.max(maxlen, curlen);
                map.put(chars[index], index);
                index++;
            }
            return maxlen;
        }
        return maxlen;
    }

    public static void main(String[] args) {
        new Q().showName(new File("/"));
    }

}

